package com.holly.unit.deform.util;

import cn.hutool.core.util.StrUtil;
import com.holly.unit.deform.entity.DesignFormOnlineField;
import com.holly.unit.deform.entity.DesignFormOnlineIndex;

import java.util.ArrayList;
import java.util.List;

public class DbTypeUtil {

    /**
     * 创建生成表的SQL
     * @param fields 字段数据
     * @param tableName  表名
     * @param tableTxt 表描述
     * @return
     */
    public static String createSQL(List<DesignFormOnlineField> fields, String tableName, String tableTxt){
        // 创建 StringBuffer 拼接sql
        StringBuffer sb = new StringBuffer();
        sb.append("CREATE TABLE `"+ tableName +"` (\n");
        List<String> columnsKey  = new ArrayList<>();
        for (int i = 0; i < fields.size(); i++) {
            DesignFormOnlineField onlineField = fields.get(i);
            String column = javaTypeColumnType(onlineField);
            sb.append(column+",\n");
            //判断是否是主键
            if(onlineField.getDbIsKey() == 1){
                columnsKey.add(judgeNumber(onlineField.getDbFieldName()));
            }
        }
        sb.append("PRIMARY KEY ("+ String.join(",",columnsKey) +")\n");
        if (StrUtil.isNotEmpty(tableTxt)){
            sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='"+tableTxt+"';");
        }else {
            sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
        }
        return sb.toString();
    }

    /**
     * 类描述: JAVA字段类型转化成数据库属性类型
     * @date 2022/1/15 14:25
     * @param field
     */
    public static String javaTypeColumnType(DesignFormOnlineField field) {
        Integer dbIsNull = field.getDbIsNull();
        String isnull = "DEFAULT NULL COMMENT ";
        if(dbIsNull == 0){
            isnull = "NOT NULL COMMENT ";
        }
        String dataType = field.getDbType();
        if (dataType.contains("string")) {
            dataType = " varchar("+field.getDbLength()+") ";
        } else if (dataType.contains("Date")) {
            dataType = " datetime ";
        } else if (dataType.contains("Text")) {
            dataType = " text ";
        } else if (dataType.contains("Blob")) {
            dataType = " Blob ";
        } else if (dataType.contains("double")) {
            dataType = " double("+field.getDbLength()+","+field.getDbPointLength()+") ";
        } else if (dataType.contains("int")) {
            dataType = " int("+field.getDbLength()+") ";
        } else if (dataType.contains("BigDecimal")) {
            dataType = " decimal("+field.getDbLength()+","+field.getDbPointLength()+") ";
        }
        return judgeNumber(field.getDbFieldName()) + dataType + isnull +judgeIsNumber(field.getDbFieldTxt());
    }

    public static String judgeIsNumber(String text){
        return "'"+text+"'";
    }

    public static String judgeNumber(String text){
        return "`"+text+"`";
    }

    /**
     * 创建索引sql
     * */
    public static List<String> createIndex(String tbname, List<DesignFormOnlineIndex> indexes) {
        List<String> list = new ArrayList<>();
        for(DesignFormOnlineIndex cgformIndex : indexes){
            String sql = "";
            String indexName = cgformIndex.getIndexName();
            String indexField = cgformIndex.getIndexField();
            String indexType = "normal".equals(cgformIndex.getIndexType())?" index ":cgformIndex.getIndexType()+ " index ";
            //TODO 确认创建索引的语句是否是一样的,如果一样 没必要判断了
           /* switch (databaseType) {
                case DataBaseConstants.DB_TYPE_MYSQL:
                    sql = "create " + indexType  + indexName + " on " + tbname + "(" + indexField + ")";
                    break;
                case DataBaseConstants.DB_TYPE_ORACLE:
                    sql = "create " + indexType  + indexName + " on " + tbname + "(" + indexField + ")";
                    break;
                case DataBaseConstants.DB_TYPE_SQLSERVER:
                    sql = "create " + indexType + indexName + " on " + tbname + "(" + indexField + ")";
                    break;
                case DataBaseConstants.DB_TYPE_POSTGRESQL:
                    sql = "create " + indexType + indexName + " on " + tbname + "(" + indexField + ")";
                    break;
                default:
                    sql = "create " + indexType + indexName + " on " + tbname + "(" + indexField + ")";
                    break;
            }*/
            sql = "create " + indexType  + indexName + " on " + tbname + "(" + indexField + ")";
            list.add(sql);
        }
        return list;
    }

}
